package tests_dao;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import dao.CrudService;
import dao.CrudServiceImpl;
import entities.Currency;
import entities.ExchangeRate;

public class DeleteTest
{
	CrudService			crud_service	= new CrudServiceImpl();
	SimpleDateFormat	sdf				= new SimpleDateFormat("dd-MM-yyyy");

	@Test
	public void deleteTest () throws ParseException
	{
		
		
		crud_service.addExchangeRate(new ExchangeRate(sdf.parse("01-02-2010"), new Currency("TST", "test"), 1, new BigDecimal(6)));
		crud_service.addExchangeRate(new ExchangeRate(sdf.parse("01-03-2010"), new Currency("TST", "test"), 1, new BigDecimal(6)));
		crud_service.addExchangeRate(new ExchangeRate(sdf.parse("01-04-2010"), new Currency("DEL", "test"), 1, new BigDecimal(6)));
		crud_service.addExchangeRate(new ExchangeRate(sdf.parse("01-05-2010"), new Currency("DEL", "test"), 1, new BigDecimal(6)));
		crud_service.addExchangeRate(new ExchangeRate(sdf.parse("01-06-2010"), new Currency("DEL", "test"), 1, new BigDecimal(6)));
		
		crud_service.deleteExchangeRate("DEL", sdf.parse("01-05-2010"));
		
		Session session = new Configuration().configure().buildSessionFactory().openSession();
		Long result = (Long) session.createQuery("select count(*) from ExchangeRate where currency.currency_code = 'DEL'").uniqueResult();
		
		Assert.assertEquals(result, new Long("2"));
	}

	@BeforeMethod
	public void beforeTest () throws ParseException
	{
		crud_service.clearDataBase();
	}

	@AfterClass
	public void afterClass ()
	{
		crud_service.clearDataBase();
	}
}
